package com.goods.business.service.imp;

import com.goods.business.mapper.BizCategoryMapper;
import com.goods.business.service.BizCategoryService;
import com.goods.common.model.business.BizProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BizCategoryServiceImpl implements BizCategoryService {

    @Autowired
    private BizCategoryMapper bizCategoryMapper;

    public PageVO getCategoryTreeByMap1(Integer pageNumber, Integer pageSize) {
        PageVO pageVO = null;
        Example example = new Example(BizProductCategory.class);
        example.createCriteria().andEqualTo("pid", 0);
        Integer offset = (pageNumber-1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);
        List<BizProductCategory> bizProductCategories = bizCategoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS1 = new ArrayList<>();
        int totalCount = bizCategoryMapper.selectCountByExample(example);
        if (!CollectionUtils.isEmpty(bizProductCategories)) {
            for (BizProductCategory bizProductCategory : bizProductCategories) {
                //封装一级分类
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(bizProductCategory, productCategoryTreeNodeVO1);
                productCategoryTreeNodeVOS1.add(productCategoryTreeNodeVO1);
                //查询子类
                example.clear();
                example.createCriteria().andEqualTo("pid", bizProductCategory.getId());
                List<BizProductCategory> bizProductCategoriesChildren = bizCategoryMapper.selectByExample(example);
                //封装子类
                ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
                for (BizProductCategory bizProductCategoriesChild : bizProductCategoriesChildren) {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO2 = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(bizProductCategoriesChild, productCategoryTreeNodeVO2);
                    productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO2);
                    //查询三级分类
                    example.clear();
                    example.createCriteria().andEqualTo("pid", bizProductCategoriesChild.getId());
                    List<BizProductCategory> bizProductCategories3 = bizCategoryMapper.selectByExample(example);
                    ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS3 = new ArrayList<>();
                    for (BizProductCategory productCategory : bizProductCategories3) {
                        ProductCategoryTreeNodeVO productCategoryTreeNodeVOChildren3 = new ProductCategoryTreeNodeVO();
                        BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVOChildren3);
                        productCategoryTreeNodeVOS3.add(productCategoryTreeNodeVOChildren3);
                    }
                    //放入三级分类
                    productCategoryTreeNodeVO2.setChildren(productCategoryTreeNodeVOS3);
                }
                //放入二级分类
                productCategoryTreeNodeVO1.setChildren(productCategoryTreeNodeVOS);
            }
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = CategoryTreeBuilder.build(productCategoryTreeNodeVOS1);
            pageVO = new PageVO<>(totalCount, productCategoryTreeNodeVOList);
        }
        return pageVO;
    }

    @Override
    public PageVO getCategoryTreeByMap(Map<String,Object> paramMap){

        Example example = new Example(BizProductCategory.class);
        List<BizProductCategory> bizProductCategories = bizCategoryMapper.selectByExample(example);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(bizProductCategory,productCategoryTreeNodeVO);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        if (paramMap.get("pageNum") !=null && paramMap.get("pageSize") != null){
             build = ListPageUtils.page(build, Integer.parseInt((String) paramMap.get("pageSize")), Integer.parseInt((String) paramMap.get("pageNum")));
        }
       PageVO<ProductCategoryTreeNodeVO> pageVO = new PageVO<>(bizProductCategories.size(),build);
        return pageVO;

    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<BizProductCategory> bizProductCategories = bizCategoryMapper.selectByExample(new Example(BizProductCategory.class));
        ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (BizProductCategory bizProductCategory : bizProductCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(bizProductCategory, productCategoryTreeNodeVO);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
    }

    @Override
    public void saveBizProductCategory(ProductCategoryVO productCategoryVO) {
        BizProductCategory bizProductCategory = new BizProductCategory();
        BeanUtils.copyProperties(productCategoryVO, bizProductCategory);
        bizCategoryMapper.insert(bizProductCategory);
    }

    @Override
    public void updateBizProductCategory(Integer id,ProductCategoryVO productCategoryVO) {
        BizProductCategory bizProductCategory = bizCategoryMapper.selectByPrimaryKey(id);
        if (bizProductCategory != null) {
            BeanUtils.copyProperties(productCategoryVO, bizProductCategory);
            bizProductCategory.setId(id.longValue());
            bizCategoryMapper.updateByPrimaryKey(bizProductCategory);
        }
    }

    @Override
    public ProductCategoryTreeNodeVO getCategoryProById(Integer id) {
        BizProductCategory bizProductCategory = bizCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        BeanUtils.copyProperties(bizProductCategory, productCategoryTreeNodeVO);
        return productCategoryTreeNodeVO;
    }

    @Override
    public void deleteCategoryProductById(Integer id) {
        bizCategoryMapper.deleteByPrimaryKey(id);
    }
}
